package com.bangyo.common.panelet.impl;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.struts.upload.FormFile;
import org.sothis.core.panelet.Panelet;

public abstract class AbstractUploadFilePanelet<F> implements Panelet<F>
{
    protected byte[] getUploadedFileContent(FormFile formFile)
    {
        if (null==formFile)
        {
            return null;
        }
        try
        {
            InputStream stream = formFile.getInputStream();
            byte[] buffer = new byte[4*1024];
            int length = 0;
            ByteArrayOutputStream byteArrayOs = new ByteArrayOutputStream(
                formFile.getFileSize());
            while ((length = stream.read(buffer))!=-1)
            {
                byteArrayOs.write(buffer, 0, length);
            }
            stream.close();
            return byteArrayOs.toByteArray();
        }
        catch (Exception e)
        {
            return null;
        }
    }

}
